package imageDemo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.HBox;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

// 图片模式
public class ImagePatternDemo extends Application {
	private HBox root = new HBox(50);
	private Rectangle rectangle1;
	private Rectangle rectangle2;
	private ImagePattern ip1;
	private ImagePattern ip2;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		Image image = new Image("image/0.jpg");
		
		// 图片在节点上开始填充的坐标(其余部分自动填充),非按比例填充时的尺寸;还可以用Label的setGraphic(control才有)
		ip1 = new ImagePattern(image, 150, 0, 300, 200, false);		
		rectangle1 = new Rectangle(300, 200);
		rectangle1.setFill(ip1);		// 颜色填充或图片填充
		
		ip2 = new ImagePattern(image, 0, 0, 1, 1, true);	// 图片在节点上开始填充的坐标,按比例填充时的尺寸比例
		rectangle2 = new Rectangle(300, 200);
		rectangle2.setFill(ip2);
		root.getChildren().addAll(rectangle1, rectangle2);
		
		Scene scene = new Scene(root, 800, 500);
		stage.setScene(scene);
		stage.show();
	}
}
